import {relationTypeList} from "./miniQueryRelationDefault";
import {Util,I18Parse} from '@lowcode/jpaas-common-lib';
import miniQueryConfig from "@/views/modules/form/core/miniQuery/config/miniQueryConfig.vue";

import codeGenSettingLang from '../../../../../locales/lang/impl/form/codeGenSettingLang';
let lang = codeGenSettingLang;
let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang.codeGenSetting,langKey, text)
}

export default {
    props: ["layerid", "destroy"],
    data(){
        return {
            labelCol: { span:6 },
            wrapperCol: { span:18 },
            labelCol1: { span:3 },
            wrapperCol1: { span:19 },
            form: this.$form.createForm(this),
            mdl:{},
            opData:[],
            opStrData:[
                {'value':'EQ','label':i18n('equal','等于')},
                {'value':'NEQ','label':i18n('notEqual','不等于')},
                {'value':'IN','label':i18n('include','包含')},
                {'value':'LK','label':i18n('fuzzyMatch','模糊匹配')},
                {'value':'LEK','label':i18n('leftMatch','左模糊匹配')},
                {'value':'RIK','label':i18n('rightMatch','右模糊匹配')}],
            opNumData:[
                {'value':'EQ','label':i18n('equal','等于')},
                {'value':'NEQ','label':i18n('notEqual','不等于')},
                {'value':'IN','label':i18n('include','包含')},
                {'value':'LT','label':i18n('lessThan','小于')},
                {'value':'LE','label':i18n('lessOrEqual','小于等于')},
                {'value':'GT','label':i18n('greater','大于')},
                {'value':'GE','label':i18n('greatOrEqual','大于等于')}],
            opDateData:[
                {'value':'EQ','label':i18n('equal','等于')},
                {'value':'NEQ','label':i18n('notEqual','不等于')},
                {'value':'LT','label':i18n('lessThan','小于')},
                {'value':'LE','label':i18n('lessOrEqual','小于等于')},
                {'value':'GT','label':i18n('greater','大于')},
                {'value':'GE','label':i18n('greatOrEqual','大于等于')}],
            fieldDatas:[],
            showDefault:false,
            showRelation:false,
            gridData:[],
            i18nDefault:process.env.VUE_APP_LANGUAGE,
            i18nCurrent:"",
        }
    },
    created() {
        this.getCurrentI18n();
    },
    methods:{
        getCurrentI18n(){
            this.i18nCurrent= window.localStorage.getItem("language");
            if(!this.i18nCurrent){
                this.i18nCurrent=this.i18nDefault;
            }
        },
        setData(data, fieldDats,queryAry,showDefault,showRelation) {
            this.mdl=data;
            this.fieldDatas=fieldDats;
            this.showDefault=showDefault;
            this.showRelation=showRelation;
            if(this.showRelation){
                this.gridData=this.mdl.gridData || [];
            }
            this.setOpData(this.mdl.dataType);
            if(this.customSetData){
                this.customSetData(data, fieldDats,queryAry);
            }
            this.form.setFieldsValue(this.mdl)
        },
        validate(callback){
            var self=this;
            this.form.validateFields((err, values) => {
                if(err){
                    return;
                }
                if(this.customValidate && !this.customValidate(values)){
                    return;
                }
                self.mdl=values;
                callback();
            });
        },
        getData(){
            if(!this.mdl['multiSelect']){
                this.mdl['cascade'] = false
            }
            if(this.mdl['dicKey']){
                var dicJson = JSON.parse(this.mdl['dicKey']);
                this.$set(this.mdl,"dicKey",dicJson.value);
                this.$set(this.mdl,"dicName",dicJson.text);
            }

            var fieldName=this.mdl['fieldName'];
            for(var i=0;i<this.fieldDatas.length;i++){
                if(this.fieldDatas[i].value==fieldName){
                    this.mdl['fieldName_name']=this.fieldDatas[i].label;
                    this.mdl['fieldExt']={value:fieldName,label:this.fieldDatas[i].label};
                    this.mdl['dataType']=this.fieldDatas[i].key;
                    this.mdl['dataType_name']=this.getDataTypeName(this.fieldDatas[i].key);
                    break;
                }
            }
            var fieldOp=this.mdl['fieldOp'];
            for(var i=0;i<this.opData.length;i++){
                if(this.opData[i].value==fieldOp){
                    this.mdl['fieldOp_name']=this.opData[i].label;
                    this.mdl['fieldOpExt']={value:fieldOp,label:this.opData[i].label};
                    break;
                }
            }
            if(this.customGetData){
                this.customGetData();
            }
            if(this.showRelation){
                this.mdl['gridData']=this.gridData;
            }
            return this.mdl;
        },
        setOpData(dataType){
            if(dataType==null || dataType=='string' || dataType=='varchar' || dataType=='clob'){
                this.opData=this.opStrData;
            }
            if((dataType=='int' || dataType=='number' || dataType=='decimal')&& this.mdl.fc!='rangepicker'){
                this.opData=this.opNumData;
            }
            if((dataType=='date' || dataType=='datetime') && this.mdl.fc!='rangepicker'){
                this.opData=this.opDateData;
            }
        },
        fieldChange(value){
            for(var i=0;i<this.fieldDatas.length;i++){
                if(this.fieldDatas[i].value==value){
                    this.setOpData(this.fieldDatas[i].key);
                    this.form.setFieldsValue({fieldLabel:this.fieldDatas[i].label})
                }
            }
        },
        getDataTypeName(dataType) {
            switch (dataType) {
                case 'string':
                    return i18n('character','字符')
                case 'int':
                    return i18n('integer','整型')
                case 'number':
                    return i18n('float','浮点型')
                case 'date':
                    return i18n('dateType','日期型')
                case 'boolean':
                    return i18n('boolean','布尔型')
                case 'currency':
                    return i18n('moneyType','货币型')
                default:
                    return ''
            }
        },
        addField(){
            this.gridData.push({value:Util.randomId(),label:"",setting:""});
        },
        addDefaultFields(type){
            let ary=relationTypeList[type];
            ary.forEach(item=>{
                for(let i=0;i<this.gridData.length;i++){
                    if(item.value==this.gridData[i].value){
                        return;
                    }
                }
                this.gridData.push(Object.assign({isDefault:true},item));
            })
        },
        configField(fieldData,type){
            var conf = {
                component: miniQueryConfig,
                title: i18n('configuration','相对值脚本配置'),
                curVm: this,
                data: {row: fieldData,fc:type},
                widthHeight: ['800px', '400px']
            };
            Util.open(conf, function (action) {
                if (action != 'ok') {
                    return;
                }
                var data = this.getData();
                Object.assign(fieldData,data);
            })
        },
        removeField(index){
            this.gridData.splice(index, 1);
        }
    }
}